<?php
// +----------------------------------------------------------------------
// | LIKE [ JUST DO IT ]
// +----------------------------------------------------------------------
// | Author: Mr.hu <huhaiyang7788@163.com>
// +----------------------------------------------------------------------
// | DESC: 日志记录信息
// +----------------------------------------------------------------------


namespace api\controllers;



use common\controllers\Functions;
use common\logs\mongo\Autoincrement;
use common\logs\mongo\MongoSave;
use yii\base\ErrorHandler;
use yii\web\Response;

class ErrorController extends ErrorHandler
{

    /**
     * 自己捕获错误信息进行保存
     * @param \Exception $exception
     * return void
     * @ Mr.hu.
     */
    public function renderException($exception)
    {

        //以下是保存的错误信息
        $mongo_log=(new MongoSave())->getCollection();
        $mongo_autoincrement=new Autoincrement();

        $insert_data=[
            '_id'=>$mongo_autoincrement->incByTableName(MongoSave::$log_table_name,'_id'),
            'level' => 0,
            'line'=>$exception->getLine(),
            'category' => $exception->getFile(),
            'message' => $exception->getMessage(),
            'create_time' => time(),
            'format_time'=>Functions::dateTime(),
            'is_readed'=>0
        ];
        $mongo_log->insert($insert_data);

        //按照原有的返回错误信息
        $response = \Yii::$app->getResponse();
        $response->format=Response::FORMAT_JSON;
        $response->data = ['code'=>-2];
        $response->send();
    }


}